<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title id="title">数据增加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../res/layui/css/layui.css"  media="all">
    <script src="../../res/js/jquery-3.1.1.min.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style type="text/css">
        .downpanel .layui-select-title span{
            line-height: 38px;
        }
        .downpanel dl dd:hover{background-color: inherit;}
    </style>
    <script type="text/javascript">
        $.ajaxSetup({
            async: false
        });
    </script>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend id="legend">数据增加</legend>
</fieldset>

<form class="layui-form" action="">
    <input type="hidden" id="itemId" name="itemId">
    <input type="hidden" id="sport" name="sport">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">指标名称</label>
            <div class="layui-input-inline">
                <input type="text" id="itemExp" name="itemExp" lay-verify="title" autocomplete="off" placeholder="请输入指标名称" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">指标单位</label>
            <div class="layui-input-inline">
                <input type="text" id="itemUnit" name="itemUnit" autocomplete="off" placeholder="请输入指标单位" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上级指标</label>
        <div class="layui-input-inline">
            <input type="text" id="parentId" lay-filter="parentId" name="parentId" class="layui-input" value="0" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">运动项目</label>
        <div class="layui-input-block" id="checkbox">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入备注" class="layui-textarea" name="remark" id="remark"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn site-demo-active" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="../../res/layui/layui.js" charset="utf-8"></script>
<script src="js/tools.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    var boole = true ;
    layui.config({
        base: 'module/'
    }).extend({
        treeSelect: 'treeSelect/treeSelect',
    }).use(['form', 'layedit', 'laydate' ,'treeSelect'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,$=layui.jquery
            ,tree=layui.tree
            ,treeSelect = layui.treeSelect;
            treeSelect.render({
                // 选择器
                elem: '#parentId',
                // 数据 
                data:'../../atsBaseItem/treeNode?isva=1' ,
                // 请求头
                headers: {},
                // 异步加载方式：get/post，默认get
                type: 'get',
                // 占位符
                placeholder: '不选择默认为顶级',
                //连线
                showLine: true,
                // 点击回调
                click: function(d){
                    console.log(d);
                },
                // 加载完成后的回调函数
                success: function (d) {
                    console.log(d);
                }
            });
        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 1){
                    return '标题至少得1个字符啊';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

            //监听提交
            form.on('submit(demo1)', function (data) {
                // 单击之后提交按钮不可选,防止重复提交
                var DISABLED = 'layui-btn-disabled';
                // 增加样式
                $('.site-demo-active').addClass(DISABLED);
                // 增加属性
                $('.site-demo-active').attr('disabled', 'disabled');
                    var id = "";
                    $("input[name='sports']:checked").each(function (index, items) {
                        id += $(this).val() + ",";
                    });
                    data.field.sport = id;
                    data.field.isva = 1 ;
                    var url = "../../atsBaseItem/insert";
                    $.post(url, data.field, function (obj) {
                        if (obj == 1) {
                            layer.msg('操作成功', {
                                icon: 1,
                                time: 1000,
                            }, function () {
                                var indexs = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(indexs);
                                parent.renderTable();
                            });
                        } else {
                            layer.msg('操作失败');
                        }
                    }, 'json')
                    return false;
            });

        var id = getQueryString("id");
        if(id!=null){
            $("#title").html("数据修改");
            $("#legend").html("数据修改");
            var urll = '../../atsBaseItem/findSport';
            var datas = {id:id};
            var idse = "";
            $.post(urll,datas,function(objs){
                for(var i = 0 ; i<objs.length ; i++){
                    idse += objs[i].sportId+",";
                }
            },'json')
            var urls = '../../atsBaseItem/findSportAll';
            $.post(urls,null,function(obj){
                $.each(obj,function(index,items){
                    var str = 0 ;
                    var idshu = idse.split(",");
                    for(var i = 0 ; i<idshu.length ; i++){
                        if(items.sportId == idshu[i]){
                            str = 1 ;
                            break ;
                        }
                    }
                    if(str == 1){
                        $("#checkbox").append('<input type="checkbox" class="checkbox" name="sports" title="'+items.sportName+'" value="'+items.sportId+'" checked="">');
                        form.render("checkbox")
                    }else{
                        $("#checkbox").append('<input type="checkbox" class="checkbox" name="sports" title="'+items.sportName+'" value="'+items.sportId+'">');
                        form.render("checkbox")
                    }
                });
            },'json');
    var url = "../../atsBaseItem/findById";
    var data = {id:id};
            $.post(url,data,function(obj){
                $("#itemId").val(obj.itemId);
                $("#itemExp").val(obj.itemExp);
                $("#itemUnit").val(obj.itemUnit);
                if(obj.parentId!=0){
                    $("#parentId").val(obj.parentId);
                    var url = "../../atsBaseItem/findById";
                    var data = {id:obj.parentId};
                    $.post(url,data,function(objs){
                        $("#treeSelect-input-lhc1234").val(objs.itemExp);
                    },'json')
                }
                $("#remark").val(obj.remark);
            },'json');
        }else{
            var url = '../../atsBaseItem/findSportAll';
            $.post(url,null,function(obj){
                $.each(obj,function(index,items){
                    $("#checkbox").append('<input type="checkbox" class="checkbox" name="sports" title="'+items.sportName+'" value="'+items.sportId+'">');

                });
                form.render("checkbox")
            },'json');
        }
    });
</script>

</body>
</html>